//
//  CZNetWorkTools.swift
//  WeiBoNew
//
//  Created by 秦川 on 16/5/12.
//  Copyright © 2016年 秦川. All rights reserved.
//

import UIKit
import AFNetworking
enum RequsetType: Int {
    case GET = 0
    case POST = 1

}


class CZNetWorkTools: AFHTTPSessionManager {

    static let shareTools: CZNetWorkTools = {
    let tool = CZNetWorkTools()
        tool.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
    return tool
    
    }()
    
    func request(requestType:RequsetType,url: String,params: AnyObject?,callback: ((response: AnyObject?,error: NSError?) ->())){
        let successClosure = {(dataTask: NSURLSessionDataTask, response: AnyObject?) -> Void in
            callback(response: response ,error: nil)
        }
        let errorClosure = {(dataTask: NSURLSessionDataTask?, error: NSError) -> Void in
            callback(response: nil ,error: error)
        }
        if requestType == .GET{
        GET(url, parameters: params, progress: nil, success: successClosure, failure: errorClosure)
        }else{
         POST(url, parameters: params, progress: nil, success: successClosure, failure: errorClosure)
        }
//        if requestType == .GET{
//        GET(url, parameters: params, progress: nil, success: { (_, response) -> Void in
//            callback(response: response ,error: nil)
//            }, failure: { (_, error) -> Void in
//              callback(response: nil ,error: error)
//        })
//        }else
//       {
//            POST(url, parameters: params, progress: nil, success: { (_, response) -> Void in
//                 callback(response: response ,error: nil)
//                }, failure: { (_, error) -> Void in
//                  callback(response: nil ,error: error)   
//            })
//        }
    
    }
    
}
//  首页相关接口
extension CZNetWorkTools{
    //微博首页数据获取
    func requestStatus(accessToken: String,callback: ((response: AnyObject?,error: NSError?) ->())){
    let url = "https://api.weibo.com/2/statuses/friends_timeline.json"
        let params = [
        
            "access_token" : accessToken
        ]
    
    
     request(.GET, url: url, params: params, callback: callback)
    
    }




}

//  OAuth相关接口
extension CZNetWorkTools{
     //  获取accesstoken的接口
    func requestAccessToken(code: String,callback: ((response: AnyObject?,error: NSError?) ->())){
        let url = "https://api.weibo.com/oauth2/access_token"
        let params = [
            "client_id" : WeiboAppKey,
            "client_secret" :WeiboAppSecret,
            "grant_type" : "authorization_code",
            
            "code" :code,
            "redirect_uri": WeiboRedirect_Uri
        ]
       request(.POST, url: url, params: params, callback: callback)
    }
    
    //  获取用户信息
    func requestUserInfo(userAccount: CZUserAcount,callback: ((response: AnyObject?,error: NSError?) ->())){
        let url = "https://api.weibo.com/2/users/show.json"
        let params = [
            "access_token": userAccount.access_token!,
            "uid": "\(userAccount.uid)"
        ]
         request(.GET, url: url, params: params, callback: callback)
        
    }
    
    
    
}









